package com.example.multithreadload;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import android.os.Handler;
import android.text.TextUtils;

public class DownLoader {

    private final int DEFAUL_TTHREAD_COUNT = 2;
    private ExecutorService mExecutors;
    private DownLoadStatus mDownLoadStatus;
    private Handler mHandler;
    private String mfilePath;
    private long mfileSize;

    private DownLoader() {
        // TODO Auto-generated constructor stub
        throw new IllegalAccessError("Can not new DownLoader");
    }

    class Builder {
        private int threadCount;
        private Handler handler;
        private String filePath;

        public void setmThreadCount(int mThreadCount) {
            this.threadCount = mThreadCount;
        }

        public void setmHandler(Handler mHandler) {
            this.handler = mHandler;
        }

        public void setFilePath(String filePath) {
            this.filePath = filePath;
        }

        public DownLoader create() {
            DownLoader downLoader = new DownLoader();
            downLoader.mDownLoadStatus = new DownLoadStatus();
            if (threadCount != 0) {
                mExecutors = Executors.newFixedThreadPool(threadCount);
            }else {
                mExecutors = Executors.newFixedThreadPool(DEFAUL_TTHREAD_COUNT);
            }

            if (handler == null) {
                throw new IllegalArgumentException("need to set Handler");
            } else {
                downLoader.mHandler = handler;
            }

            if (TextUtils.isEmpty(filePath)) {
                throw new IllegalArgumentException("need to set fileName");
            } else {
                downLoader.mfilePath = filePath;
            }

            return downLoader;
        }
    }

    class DownLoadRunnableNow implements Runnable {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            
        }
    }

    class DownLoadRunnable implements Runnable {

        private String urlString;

        public DownLoadRunnable(String urlString) {
            super();
            this.urlString = urlString;
        }

        @Override
        public void run() {
            // TODO Auto-generated method stub
            mfileSize = getFileSizeSync(urlString);
            downloadNow();
        }
    }

    public void download(String url) {
        if(mDownLoadStatus.isStatus(DownLoadStatus.DOWNLOADING)) {
            return;
        }
        new Thread().interrupt();
    }

    public void downloadNow() {
        // TODO Auto-generated method stub
    }

    private long getFileSizeSync(String urlString) {
        long fileSize = 0;
        try {
            URL url = new URL(urlString);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setReadTimeout(5 * 1000);
            connection.setRequestMethod("GET");

            fileSize = connection.getContentLength();
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return fileSize;
    }
}
